# This file takes in a HUGO ID and a PDB ID from the command line
# and outputs a high-quality image of the PDB file
# Requires PyMOL and GIMP 2.6 executable from 
# the command line; details noted below.

# PyMOL (version 0.99rc6): http://delsci.com/rel/099/
# GIMP 2.6: http://www.gimp.org/downloads/

import urllib.request, optparse, os, re, subprocess
from optparse import OptionParser
import time

# emw: Begins clock for timing how long it takes
# to go from input PDB ID to output	finished image
t_init = time.time()

renderingRE_1 = re.compile("rendering based on \w{4}")
renderingRE_2 = re.compile("rendering based on")

def parse_args():
	optionParser = optparse.OptionParser()
	optionParser.add_option("--pdb_id", dest="pdb_id")
	optionParser.add_option("--protein", dest="protein_abbreviation")
	(options, args) = optionParser.parse_args()
	return (str(options.pdb_id), str(options.protein_abbreviation))

def download_pdb_file(pdb_id):
	# Downloads requested PDB file to current directory
	# Possible to do: download local PDB database and query that
	fp = urllib.request.urlopen("http://www.rcsb.org/pdb/files/" + pdb_id + ".pdb")
	mybytes = fp.read()
	# Note that Python3 does not read the html code as string
	# but as bytearray
	pdbString = str(mybytes.decode("utf8"))
	fp.close()
	open(pdb_id + "." + protein_abbreviation + ".pdb","w").write(pdbString)
	return

pdb_id, protein_abbreviation = parse_args()

t0 = time.time()
download_pdb_file(pdb_id)
print("    download_pdb_file(pdb_id): %.2f s" % (time.time() - t0))

t0 = time.time()
# For the below subprocess to work, the location of the PyMOL application
# (e.g. C:\Program Files\DeLano Scientific\PyMOL) must be added to the
# PATH environment variable
# This subprocess converts the .pdb file to a nice-looking .png image file
subprocess.Popen("pymol -c -q pymolProcessor.py", shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
print("    PyMOL processing: %.2f s" % (time.time() - t0))



# Takes in a PNG and outputs an image with a 50 px buffered autocrop.
			
# As with PyMOL, the location of GIMP must be added to the PATH
# environment variable for the subprocess below to work.

# The custom script for 'crop-and-pad-pdb' is in C:\Windows\Users\{administrator account}\.gimp-2.6\scripts\
# http://www.gimp.org/tutorials/Basic_Batch/ was quite helpful here.

# The batch call below makes this incompatible with Unix environments.
# The issue here is how the quotes are escaped -- using a call like the one
# outlined at the bottom of http://www.gimp.org/tutorials/Basic_Batch/ 
# should remedy the situation for Unix-based users.
t0 = time.time()
subprocess.Popen("gimp-console-2.6 --no-fonts --no-data --no-interface --batch \"(crop-and-pad-pdb \\\"images_for_release/Protein_" + protein_abbreviation + "_PDB_" + pdb_id + ".png\\\")\" -b \"(gimp-quit 0)\"", \
shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
print("    GIMP batch call: %.2f s" % (time.time() - t0))


# Deletes residual pdb file; there is no 
# apparent reason to keep it lingering around
os.system("del " + pdb_id + "." + protein_abbreviation + ".pdb")

print("    Total time: %.2f s \n" % (time.time() - t_init))